package com.ruoyi.msg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.msg.domain.MsgZnx;
import com.ruoyi.msg.domain.bo.MsgZnxBo;
import com.ruoyi.msg.domain.vo.MsgZnxVo;
import com.ruoyi.msg.mapper.MsgZnxMapper;
import com.ruoyi.msg.service.IMsgZnxService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 站内信Service业务层处理
 *
 * @author xiaohao
 * @date 2023-02-01
 */
@RequiredArgsConstructor
@Service
public class MsgZnxServiceImpl implements IMsgZnxService {

    private final MsgZnxMapper baseMapper;

    /**
     * 查询站内信
     */
    @Override
    public MsgZnxVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询站内信列表
     */
    @Override
    public TableDataInfo<MsgZnxVo> queryPageList(MsgZnxBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MsgZnx> lqw = buildQueryWrapper(bo);
        Page<MsgZnxVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询站内信列表
     */
    @Override
    public List<MsgZnxVo> queryList(MsgZnxBo bo) {
        LambdaQueryWrapper<MsgZnx> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MsgZnx> buildQueryWrapper(MsgZnxBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MsgZnx> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), MsgZnx::getTitle, bo.getTitle());
        lqw.eq(bo.getDeadlineTime() != null, MsgZnx::getDeadlineTime, bo.getDeadlineTime());
        return lqw;
    }

    /**
     * 新增站内信
     */
    @Override
    public Boolean insertByBo(MsgZnxBo bo) {
        MsgZnx add = BeanUtil.toBean(bo, MsgZnx.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改站内信
     */
    @Override
    public Boolean updateByBo(MsgZnxBo bo) {
        MsgZnx update = BeanUtil.toBean(bo, MsgZnx.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MsgZnx entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除站内信
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
